import type { NextRequest } from 'next/server'

// 从cookie获取认证信息 (服务端使用)
export function getAuthInfoFromCookie(request: NextRequest): {
  password?: string
  username?: string
  signature?: string
  timestamp?: number
} | null {
  const authCookie = request.cookies.get('auth')

  if (!authCookie) {
    return null
  }

  try {
    // 处理可能的双重编码
    let decoded = decodeURIComponent(authCookie.value)

    // 如果解码后仍然包含 %，说明是双重编码，需要再次解码
    if (decoded.includes('%')) {
      decoded = decodeURIComponent(decoded)
    }

    const authData = JSON.parse(decoded)
    return authData
  }
  catch {
    return null
  }
}

// 从cookie获取认证信息 (客户端使用)
export function getAuthInfoFromBrowserCookie(): {
  password?: string
  username?: string
  signature?: string
  timestamp?: number
  role?: 'owner' | 'admin' | 'user'
} | null {
  if (typeof window === 'undefined') {
    return null
  }

  try {
    // 解析 document.cookie
    const cookies = document.cookie.split(';').reduce(
      (acc, cookie) => {
        const trimmed = cookie.trim()
        const firstEqualIndex = trimmed.indexOf('=')

        if (firstEqualIndex > 0) {
          const key = trimmed.substring(0, firstEqualIndex)
          const value = trimmed.substring(firstEqualIndex + 1)
          if (key && value) {
            acc[key] = value
          }
        }

        return acc
      },
      {} as Record<string, string>,
    )

    const authCookie = cookies.auth
    if (!authCookie) {
      return null
    }

    // 处理可能的双重编码
    let decoded = decodeURIComponent(authCookie)

    // 如果解码后仍然包含 %，说明是双重编码，需要再次解码
    if (decoded.includes('%')) {
      decoded = decodeURIComponent(decoded)
    }

    const authData = JSON.parse(decoded)
    return authData
  }
  catch {
    return null
  }
}
